package com.wx.core.process.message.event;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.wx.commons.ResultConstant;
import com.wx.core.process.BaseProcess;
import com.wx.core.process.BaseResult;
import com.wx.core.process.ProcessResult;
import com.wx.core.vo.MassMsgRequest;
import com.wx.core.vo.MsgRequest;
import com.wx.entity.WxMassSendResult;
import com.wx.entity.WxMassSendResultExample;
import com.wx.service.WxMassSendResultService;

/**
 * 描述：微信消息群发结果处理<br/>
 * 作者：stem zhang <br/>
 * 修改日期：2015年8月17日 - 下午3:53:19<br/>
 *
 */
public class MassSendJobFinishProcess extends BaseProcess {

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	private WxMassSendResultService wxMassSendResultService;
	
	@Override
	public BaseResult process(HttpServletRequest req, HttpServletResponse res, MsgRequest msgReq) {
		ProcessResult result = new ProcessResult();  
		result.setResult("");
		if(!(msgReq instanceof MassMsgRequest)){
			result.setCode(ResultConstant.ERRCODE);
			return result;
		}
		MassMsgRequest massMsgRequest = (MassMsgRequest)msgReq;
		if(logger.isDebugEnabled()){
			String json = JSON.toJSONString(massMsgRequest);
			logger.debug("公众号【"+massMsgRequest.getToUserName()+"】,消息代码["+massMsgRequest.getMsgId()+"]群发消息成功,结果是：--->\n"+json);
		}
		//记录群发结果数据
		if(null!=this.wxMassSendResultService){
			String ctime = massMsgRequest.getCreateTime();
			long lTime = 0;
			if(null!=ctime){
				lTime = Long.parseLong(ctime);
			}else{
				lTime = System.currentTimeMillis();
			}
			String wxid = massMsgRequest.getToUserName();
			WxMassSendResult bean = new WxMassSendResult();
			bean.setWxid(wxid);
			bean.setCreateTime(new Date(lTime*1000));
			bean.setErrorCount(massMsgRequest.getErrorCount());
			bean.setFilterCount(massMsgRequest.getFilterCount());
			bean.setFromUserName(massMsgRequest.getFromUserName());
			bean.setMsgId(massMsgRequest.getMsgId());
			bean.setSentCount(massMsgRequest.getSentCount());
			bean.setStatus(massMsgRequest.getStatus());
			bean.setTotalCount(massMsgRequest.getTotalCount());
			bean.setToUserName(massMsgRequest.getToUserName());
			
			WxMassSendResultExample example = new WxMassSendResultExample();
			example.createCriteria().andMsgIdEqualTo(msgReq.getMsgId());
			PageHelper.startPage(1,1);
			List<WxMassSendResult> resultList = this.wxMassSendResultService.list(example);
			if(resultList.size()>0){
				WxMassSendResult resultOne = resultList.get(0);
				BeanUtils.copyProperties(bean,resultOne);
				this.wxMassSendResultService.updateById(resultOne);
			}else{
				this.wxMassSendResultService.addOne(bean);
			}
		}else{
			logger.error("The bean named 'wxMassSendResultService' has not be injected, it will make the mass message sending state not record into the DB......");
		}
		result.setCode(ResultConstant.SUCCESS);
		return result;
	}

	
	public void setWxMassSendResultService(WxMassSendResultService wxMassSendResultService){
		this.wxMassSendResultService = wxMassSendResultService;
	}
}
